Public Class COrderStatusReport

    Private _year As Integer
    Private _week As Integer
    Private _customerID As Integer
    Private _supplierID As Integer
    Private _ETAportID As Integer
    Private _supplierName As String
    Private _ETAportName As String
    Private _totalItem As Integer
    Private _output As Report.EReportOutputType
    Private _customerName As String
    Private _supplierNameAsDisplay As String
    Private _arrivalPortAsDisplay As String
    Private _sortExpression As String = EOSReportColumn.OrderNumber.ToString
    Private _sortDirection As String
    Private _dataTableOSReport As DataTable



    Private _ETAPorts As System.Web.UI.WebControls.ListItemCollection
    Private _Suppliers As System.Web.UI.WebControls.ListItemCollection
    Private _bodyHTML As String

#Region "    Properties"
    Public ReadOnly Property ETAPorts() As DataView
        Get
            Dim dt As New DataTable
            dt.Columns.Add("Text")
            dt.Columns.Add("Value")
            Dim item As System.Web.UI.WebControls.ListItem

            For Each item In Me._ETAPorts
                Dim dr As DataRow = dt.NewRow
                dr("Text") = item.Text
                dr("Value") = item.Value
                dt.Rows.Add(dr)
            Next

            dt.DefaultView.Sort = "Text"

            Return dt.DefaultView
        End Get
    End Property

    Public ReadOnly Property Suppliers() As DataView
        Get
            Dim dt As New DataTable
            dt.Columns.Add("Text")
            dt.Columns.Add("Value")
            Dim item As System.Web.UI.WebControls.ListItem

            For Each item In Me._Suppliers
                Dim dr As DataRow = dt.NewRow
                dr("Text") = item.Text
                dr("Value") = item.Value
                dt.Rows.Add(dr)
            Next

            dt.DefaultView.Sort = "Text"

            Return dt.DefaultView
        End Get
    End Property

    Public Property CustomerID() As Integer
        Get
            Return _customerID
        End Get
        Set(ByVal Value As Integer)
            _customerID = Value
        End Set
    End Property

    Public ReadOnly Property OrderCustomer() As CUser
        Get
            Dim customer As New CUser
            customer.Load(_customerID)

            Return customer
        End Get
    End Property


    Public Property CustomerName() As String
        Get
            Return _customerName
        End Get
        Set(ByVal Value As String)
            _customerName = Value
        End Set
    End Property

    Public Property SupplierNameAsDisplay() As String
        Get
            Return _supplierNameAsDisplay
        End Get
        Set(ByVal Value As String)
            _supplierNameAsDisplay = Value
        End Set
    End Property

    Public Property ArrivalPortAsDisplay() As String
        Get
            Return _arrivalPortAsDisplay
        End Get
        Set(ByVal Value As String)
            _arrivalPortAsDisplay = Value
        End Set
    End Property

    Public Property SortExpression() As String
        Get
            Return _sortExpression
        End Get
        Set(ByVal Value As String)
            _sortExpression = Value
        End Set
    End Property

    Public Property SortDirection() As String
        Get
            Return _sortDirection
        End Get
        Set(ByVal Value As String)
            _sortDirection = Value
        End Set
    End Property

    Public ReadOnly Property TotalItem() As Integer
        Get
            Return _totalItem
        End Get
    End Property

    Public Property Year() As Integer
        Get
            Return _year
        End Get
        Set(ByVal Value As Integer)
            _year = Value
        End Set
    End Property

    Public Property Week() As Integer
        Get
            Return _week
        End Get
        Set(ByVal Value As Integer)
            _week = Value
        End Set
    End Property

    Public Property SupplierID() As Integer
        Get
            Return _supplierID
        End Get
        Set(ByVal Value As Integer)
            _supplierID = Value
        End Set
    End Property

    Public ReadOnly Property SupplierName() As String
        Get
            If Me.SupplierID = 0 Then
                Return "[All]"
            End If
            If IsNothing(Me._supplierName) OrElse Me._supplierName.Trim = "" Then
                Me._supplierName = Common.GetLKDValue(Me.SupplierID, 2)
            End If
            Return Me._supplierName
        End Get
    End Property

    Public Property ETAPortID() As Integer
        Get
            Return _ETAportID
        End Get
        Set(ByVal Value As Integer)
            _ETAportID = Value
        End Set
    End Property

    Public ReadOnly Property ETAPortName() As String
        Get
            If ETAPortID = 0 Then
                Return "[All]"
            End If
            If IsNothing(Me._ETAportName) OrElse Me._ETAportName.Trim = "" Then
                Me._ETAportName = Common.GetLKDValue(Me.ETAPortID, 1)
            End If
            Return Me._ETAportName
        End Get
    End Property
    ' 0: Screen (View)
    ' 1: Printer (Print)
    ' 2: File (Export)
    Public Property Output() As Report.EReportOutputType
        Get
            Return _output
        End Get
        Set(ByVal Value As Report.EReportOutputType)
            _output = Value
        End Set
    End Property

    Private ReadOnly Property BodyHTML() As String
        Get
            If IsNothing(_bodyHTML) OrElse _bodyHTML.Trim = "" Then
                _bodyHTML = Me.GetTBodyHTML
            End If
            Return _bodyHTML
        End Get
    End Property
#End Region

    Protected Overridable Function GetReportHeaderHTML() As String
        Dim sb As New System.Text.StringBuilder
        sb.Append("<TABLE border=0 width=""100%"">")
        sb.Append("<TR>")
        sb.Append("<TD align=center colspan=13 class=txtnormal><b>ORDER STATUS REPORT</b></TD>")
        sb.Append("</TR>")

        sb.Append("<TR class=txtnormal>")
        sb.AppendFormat("<td align=left colspan=6>Week: {0} / Year: {1}</td>", Me.Week, Me.Year)
        sb.AppendFormat("<td align=right colspan=7>Supplier: {0}</td>", SupplierName)
        sb.Append("</TR>")

        sb.Append("<TR class=txtnormal>")
        sb.AppendFormat("<td align=right colspan=13>ETA Port: {0}</td>", Me.ETAPortName)
        sb.Append("</TR>")
        sb.Append("</TABLE >")
        Return sb.ToString
    End Function

    Public Sub RefreshReport()
        Me._bodyHTML = Me.GetTBodyHTML
    End Sub

    Public Function GetReportHTML() As String
        Dim sb As New System.Text.StringBuilder

        If Me.Output <> Report.EReportOutputType.Screen And Me.Output <> Report.EReportOutputType.Printer Then
            sb.Append(Me.GetReportHeaderHTML)
        End If

        Select Case Me.Output
            Case Report.EReportOutputType.File
                sb.Append("<TABLE cellSpacing=1 cellPadding=3 width=""100%""  border=1>")
            Case Report.EReportOutputType.Printer
                sb.Append("<TABLE class=border border=0 cellSpacing=1 cellPadding=3 width=""100%"">")
            Case Report.EReportOutputType.Screen
                sb.Append("<TABLE class=border border=0 cellSpacing=1 cellPadding=3 width=""100%"">")
        End Select

        sb.Append(Me.GetTHeadHTML)
        sb.Append(Me.BodyHTML)
        sb.Append("</TABLE>")
        Return sb.ToString
    End Function

    Protected Overridable Function GetTHeadHTML() As String

        If (Me.Output = Report.EReportOutputType.Screen) Then
            Return GetTHeadHTMLForScreen()
        End If

        Dim sb As New System.Text.StringBuilder

        sb.Append("<TR align=""center"">")
        sb.Append("<TD class=""txtheader"" ><strong>Bulk</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>" + System.Web.HttpUtility.HtmlEncode(OrderCustomer.SortReference1) + "</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>" + System.Web.HttpUtility.HtmlEncode(OrderCustomer.SortReference2) + "</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>Date of Order</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>Qty</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>% Completed</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>Customer Order No.</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>ETD</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=150 nowrap><strong>Vessel/Voyage</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>Arrival Port</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>ETA</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>Doc's Sent</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>Payment Received</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><strong>Invoice No.</strong></TD>")
        sb.Append("</TR>")

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<THEAD>")
            sb.Append("</THEAD>")
        End If

        Return sb.ToString
    End Function

    Protected Overridable Function GetTHeadHTMLForScreen() As String

        Dim sb As New System.Text.StringBuilder

        sb.Append("<TR align=""center"">")
        sb.Append("<TD class=""txtheader"" ><strong>Bulk</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.SortReference.ToString + "');""><strong>" + System.Web.HttpUtility.HtmlEncode(OrderCustomer.SortReference1) + "</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.SortReference2.ToString + "');""><strong>" + System.Web.HttpUtility.HtmlEncode(OrderCustomer.SortReference2) + "</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.OrderDate.ToString + "');""><strong>Date of Order</strong></TD>")
        sb.Append("<TD class=""txtheader"" width=50 ><strong>Qty</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.PercentCompleted.ToString + "');""><strong>% Completed</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.OrderNumber.ToString + "');""><strong>Customer Order No.</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.ETD.ToString + "');""><strong>ETD</strong></a></TD>")
        sb.Append("<TD class=""txtheader"" width=100 nowrap><a href=""javascript:SortOnColumn('" + EOSReportColumn.Vessel.ToString + "');""><strong>Vessel/Voyage</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.DestinationPort.ToString + "');""><strong>Arrival Port</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.ETA.ToString + "');""><strong>ETA</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.DocSent.ToString + "');""><strong>Doc's Sent</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.PayReceived.ToString + "');""><strong>Payment Received</strong></TD>")
        sb.Append("<TD class=""txtheader"" ><a href=""javascript:SortOnColumn('" + EOSReportColumn.InvoiceNo.ToString + "');""><strong>Invoice No.</strong></TD>")
        sb.Append("</TR>")

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<THEAD>")
            sb.Append("</THEAD>")
        End If

        Return sb.ToString
    End Function

    Protected Overridable Function GetTBodyHTML() As String

        Dim dt As DataTable
        dt = COrderStatus.GetOrderStatusReport(Me.SupplierID, Me.ETAPortID, Me.Week, Me.Year, Me.CustomerID)
        _totalItem = dt.Rows.Count

        'Here we sort data before displaying them.
        If Me._sortExpression <> Nothing Then
            If Me._sortExpression.Length > 0 Then

                Dim dataTableOSReport As DataTable
                dataTableOSReport = BuildDataTableOSReport(dt)

                Dim dv As DataView
                dv = GetDataView(dataTableOSReport, Me._sortExpression, Me._sortDirection)

                Return Me.GetTBodyHTML(dv)
            End If
        End If

        Dim sb As New System.Text.StringBuilder
        Dim bgcolor As String
        Dim dr As DataRow
        For Each dr In dt.Rows
            Dim objLastStatus As COrderStatus
            Dim objO As New COrder
            objO.Load(dr("ORDER_ID_FK"))

            ' get supplier list and ETA Port list from report data
            ' remove duplicate data
            If IsNothing(Me._Suppliers.FindByValue(objO.SupplierLkdID)) Then
                Me._Suppliers.Add(New System.Web.UI.WebControls.ListItem(objO.SupplierCode, objO.SupplierLkdID))
            End If

            If IsNothing(Me._ETAPorts.FindByValue(objO.DestinationPortLkdID)) Then
                Me._ETAPorts.Add(New System.Web.UI.WebControls.ListItem(objO.DestinationPort, objO.DestinationPortLkdID))
            End If

            Select Case CType(dr("STATUS"), Report.EReportOrderStatus)
                Case Report.EReportOrderStatus.Additions
                    sb.Append("<tr align=center class=bgblue>")
                    bgcolor = "#F5FAFE"
                Case Report.EReportOrderStatus.Archived
                    sb.Append("<tr bgcolor=khaki align=center class=bgyellow>")
                    bgcolor = "#FDFDF2"
                Case Report.EReportOrderStatus.Removed
                    sb.Append("<tr align=center class=bggrey>")
                    bgcolor = "#F8F8F8"
                Case Report.EReportOrderStatus.Changes
                    sb.Append("<tr align=center class=bggreen>")
                    bgcolor = "#EEF9F2"
                    Dim w, y As Integer
                    If Me.Week = 1 Then
                        w = 52
                        y = Me.Year - 1
                    Else
                        w = Me.Week - 1
                        y = Me.Year
                    End If
                    objLastStatus = New COrderStatus
                    ' last week order status
                    objLastStatus.Load(objO.ID, w, y)

                Case Report.EReportOrderStatus.No_changes
                    sb.Append("<tr bgcolor=khaki align=center class=bgwhite>")
                    bgcolor = "#FFFFFF"
            End Select

            sb.AppendFormat("<td bgcolor={1}>{0}</td>", IIf(objO.IsIndependentPO, "", "*"), bgcolor)
            sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.SortReference, bgcolor)
            sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.SortReference2, bgcolor)
            sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(objO.OrderDate), bgcolor)
            sb.AppendFormat("<td bgcolor={1}>{0}</td>", String.Format("{0} x{1}", objO.Containers, CType(objO.ContainerType, EContainerType).ToString.Replace("_", " ")), bgcolor)

            If IsNothing(objLastStatus) AndAlso CType(dr("STATUS"), Report.EReportOrderStatus) <> Report.EReportOrderStatus.Changes Then
                If (dr("PERCENT_COMPLETED") Is System.DBNull.Value) Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", dr("PERCENT_COMPLETED"), bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", CInt(dr("PERCENT_COMPLETED")), bgcolor)
                End If
                sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.OrderNumber, bgcolor)

                If dr("EST_TIME_DELIVERY") Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "N/A", bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_DELIVERY"))), bgcolor)
                End If

                sb.AppendFormat("<td bgcolor={1}>{0}</td>", dr("VESSEL"), bgcolor)
                sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.DestinationPort, bgcolor)

                If dr("EST_TIME_ARRIVAL") Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "N/A", bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_ARRIVAL"))), bgcolor)
                End If

                If dr("DOC_SENT") Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "No", bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", IIf(CBool(dr("DOC_SENT")) = True, "Yes", "No"), bgcolor)
                End If

                If dr("PAY_RECEIVED") Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "No", bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", IIf(CBool(dr("PAY_RECEIVED")) = True, "Yes", "No"), bgcolor)
                End If

                sb.AppendFormat("<td bgcolor={1}>{0}</td>", dr("INVOICE_NO"), bgcolor)
            Else
                If (dr("PERCENT_COMPLETED") Is System.DBNull.Value) Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", dr("PERCENT_COMPLETED"), bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", CInt(dr("PERCENT_COMPLETED")), bgcolor, IIf(Decimal.Equals(CDec(dr("PERCENT_COMPLETED")), objLastStatus.PercentCompleted), "", "class=boldText"))
                End If
                sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.OrderNumber, bgcolor)

                'If dr("EST_TIME_DELIVERY") Is System.DBNull.Value Then
                '    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", "N/A", bgcolor, "")
                'Else
                '    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_DELIVERY"))), bgcolor, IIf(Date.Equals(CDate(dr("EST_TIME_DELIVERY")), objLastStatus.EstTimeDelivery), "", "class=boldText"))
                'End If
                If dr("EST_TIME_DELIVERY") Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "N/A", bgcolor)
                ElseIf (CDate(dr("EST_TIME_DELIVERY")).Equals(objLastStatus.EstTimeDelivery)) Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_DELIVERY"))), bgcolor)
                ElseIf (CDate(dr("EST_TIME_DELIVERY")).CompareTo(objLastStatus.EstTimeDelivery) > 0) Then
                    sb.AppendFormat("<td class='bghighlight1'>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_DELIVERY"))))
                Else
                    sb.AppendFormat("<td class='bghighlight'>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_DELIVERY"))))
                End If


                Dim strVessel As String = IIf(dr("VESSEL") Is System.DBNull.Value, "", dr("VESSEL"))
                sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", strVessel, bgcolor, IIf(String.Equals(strVessel, objLastStatus.Vessel), "", "class=boldText"))
                sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.DestinationPort, bgcolor)

                'If dr("EST_TIME_ARRIVAL") Is System.DBNull.Value Then
                '    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", "N/A", bgcolor, "")
                'Else
                '    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_ARRIVAL"))), bgcolor, IIf(Date.Equals(CDate(dr("EST_TIME_ARRIVAL")), objLastStatus.EstTimeArrival), "", "class=boldText"))
                'End If
                If dr("EST_TIME_ARRIVAL") Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "N/A", bgcolor)
                ElseIf (CDate(dr("EST_TIME_ARRIVAL")).Equals(objLastStatus.EstTimeArrival)) Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_ARRIVAL"))), bgcolor)
                ElseIf (CDate(dr("EST_TIME_ARRIVAL")).CompareTo(objLastStatus.EstTimeArrival) > 0) Then
                    sb.AppendFormat("<td class='bghighlight1'>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_ARRIVAL"))))
                Else
                    sb.AppendFormat("<td class='bghighlight'>{0}</td>", Common.FormatDate(CDate(dr("EST_TIME_ARRIVAL"))))
                End If



                If dr("DOC_SENT") Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", "No", bgcolor, IIf(Boolean.Equals("No", objLastStatus.DocSent), "", "class=boldText"))
                Else
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", IIf(CBool(dr("DOC_SENT")) = True, "Yes", "No"), bgcolor, IIf(Boolean.Equals(CBool(dr("DOC_SENT")), objLastStatus.DocSent), "", "class=boldText"))
                End If

                If dr("PAY_RECEIVED") Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", "No", bgcolor, IIf(Boolean.Equals("No", objLastStatus.PayReceived), "", "class=boldText"))
                Else
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", IIf(CBool(dr("PAY_RECEIVED")) = True, "Yes", "No"), bgcolor, IIf(Boolean.Equals(CBool(dr("PAY_RECEIVED")), objLastStatus.PayReceived), "", "class=boldText"))
                End If

                Dim strInvoiceNo As String = IIf(dr("INVOICE_NO") Is System.DBNull.Value, "", dr("INVOICE_NO"))
                sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", strInvoiceNo, bgcolor, IIf(String.Equals(strInvoiceNo, objLastStatus.InvoiceNo), "", "class=boldText"))

                objLastStatus = Nothing
            End If

            sb.Append("</tr>")
        Next

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<TBODY>")
            sb.Append("</TBODY>")
        End If

        Return sb.ToString
    End Function

    Protected Overridable Function GetTBodyHTML(ByVal dv As DataView) As String

        Dim sb As New System.Text.StringBuilder
        Dim bgcolor As String
        Dim dr As DataRow
        Dim drv As DataRowView
        Dim i As Integer
        For i = 0 To dv.Count - 1
            drv = dv(i)
            dr = drv.Row

            Dim objLastStatus As COrderStatus
            Dim objO As New COrder
            objO.Load(dr(EOSReportColumn.OrderID.ToString))

            ' get supplier list and ETA Port list from report data
            ' remove duplicate data
            If IsNothing(Me._Suppliers.FindByValue(objO.SupplierLkdID)) Then
                Me._Suppliers.Add(New System.Web.UI.WebControls.ListItem(objO.SupplierCode, objO.SupplierLkdID))
            End If

            If IsNothing(Me._ETAPorts.FindByValue(objO.DestinationPortLkdID)) Then
                Me._ETAPorts.Add(New System.Web.UI.WebControls.ListItem(objO.DestinationPort, objO.DestinationPortLkdID))
            End If

            Select Case CType(dr(EOSReportColumn.Status.ToString), Report.EReportOrderStatus)
                Case Report.EReportOrderStatus.Additions
                    sb.Append("<tr align=center class=bgblue>")
                    bgcolor = "#F5FAFE"
                Case Report.EReportOrderStatus.Archived
                    sb.Append("<tr bgcolor=khaki align=center class=bgyellow>")
                    bgcolor = "#FDFDF2"
                Case Report.EReportOrderStatus.Removed
                    sb.Append("<tr align=center class=bggrey>")
                    bgcolor = "#F8F8F8"
                Case Report.EReportOrderStatus.Changes
                    sb.Append("<tr align=center class=bggreen>")
                    bgcolor = "#EEF9F2"
                    Dim w, y As Integer
                    If Me.Week = 1 Then
                        w = 52
                        y = Me.Year - 1
                    Else
                        w = Me.Week - 1
                        y = Me.Year
                    End If
                    objLastStatus = New COrderStatus
                    ' last week order status
                    objLastStatus.Load(objO.ID, w, y)

                Case Report.EReportOrderStatus.No_changes
                    sb.Append("<tr bgcolor=khaki align=center class=bgwhite>")
                    bgcolor = "#FFFFFF"
            End Select

            sb.AppendFormat("<td bgcolor={1}>{0}</td>", IIf(objO.IsIndependentPO, "", "*"), bgcolor)
            sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.SortReference, bgcolor)
            sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.SortReference2, bgcolor)
            sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(objO.OrderDate), bgcolor)
            sb.AppendFormat("<td bgcolor={1}>{0}</td>", String.Format("{0} x{1}", objO.Containers, CType(objO.ContainerType, EContainerType).ToString.Replace("_", " ")), bgcolor)

            If IsNothing(objLastStatus) AndAlso CType(dr(EOSReportColumn.Status.ToString), Report.EReportOrderStatus) <> Report.EReportOrderStatus.Changes Then
                If (dr(EOSReportColumn.PercentCompleted.ToString) Is System.DBNull.Value) Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", dr(EOSReportColumn.PercentCompleted.ToString), bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", CInt(dr(EOSReportColumn.PercentCompleted.ToString)), bgcolor)
                End If

                sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.OrderNumber, bgcolor)

                If dr(EOSReportColumn.ETD.ToString) Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "N/A", bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(CDate(dr(EOSReportColumn.ETD.ToString))), bgcolor)
                End If

                sb.AppendFormat("<td bgcolor={1}>{0}</td>", dr(EOSReportColumn.Vessel.ToString), bgcolor)
                sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.DestinationPort, bgcolor)

                If dr(EOSReportColumn.ETA.ToString) Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "N/A", bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(CDate(dr(EOSReportColumn.ETA.ToString))), bgcolor)
                End If

                If dr(EOSReportColumn.DocSent.ToString) Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "No", bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", IIf(CBool(dr(EOSReportColumn.DocSent.ToString)) = True, "Yes", "No"), bgcolor)
                End If

                If dr(EOSReportColumn.PayReceived.ToString) Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "No", bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", IIf(CBool(dr(EOSReportColumn.PayReceived.ToString)) = True, "Yes", "No"), bgcolor)
                End If

                sb.AppendFormat("<td bgcolor={1}>{0}</td>", dr(EOSReportColumn.InvoiceNo.ToString), bgcolor)
            Else
                If (dr(EOSReportColumn.PercentCompleted.ToString) Is System.DBNull.Value) Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", dr(EOSReportColumn.PercentCompleted.ToString), bgcolor)
                Else
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", CInt(dr(EOSReportColumn.PercentCompleted.ToString)), bgcolor, IIf(Decimal.Equals(CDec(dr(EOSReportColumn.PercentCompleted.ToString)), objLastStatus.PercentCompleted), "", "class=boldText"))
                End If

                sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.OrderNumber, bgcolor)

                If dr(EOSReportColumn.ETD.ToString) Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "N/A", bgcolor)
                ElseIf (CDate(dr(EOSReportColumn.ETD.ToString)).Equals(objLastStatus.EstTimeDelivery)) Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(CDate(dr(EOSReportColumn.ETD.ToString))), bgcolor)
                ElseIf (CDate(dr(EOSReportColumn.ETD.ToString)).CompareTo(objLastStatus.EstTimeDelivery) > 0) Then
                    sb.AppendFormat("<td class='bghighlight1'>{0}</td>", Common.FormatDate(CDate(dr(EOSReportColumn.ETD.ToString))))
                Else
                    sb.AppendFormat("<td class='bghighlight'>{0}</td>", Common.FormatDate(CDate(dr(EOSReportColumn.ETD.ToString))))
                End If


                Dim strVessel As String = IIf(dr(EOSReportColumn.Vessel.ToString) Is System.DBNull.Value, "", dr(EOSReportColumn.Vessel.ToString))
                sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", strVessel, bgcolor, IIf(String.Equals(strVessel, objLastStatus.Vessel), "", "class=boldText"))
                sb.AppendFormat("<td bgcolor={1}>{0}</td>", objO.DestinationPort, bgcolor)

                If dr(EOSReportColumn.ETA.ToString) Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", "N/A", bgcolor)
                ElseIf (CDate(dr(EOSReportColumn.ETA.ToString)).Equals(objLastStatus.EstTimeArrival)) Then
                    sb.AppendFormat("<td bgcolor={1}>{0}</td>", Common.FormatDate(CDate(dr(EOSReportColumn.ETA.ToString))), bgcolor)
                ElseIf (CDate(dr(EOSReportColumn.ETA.ToString)).CompareTo(objLastStatus.EstTimeArrival) > 0) Then
                    sb.AppendFormat("<td class='bghighlight1'>{0}</td>", Common.FormatDate(CDate(dr(EOSReportColumn.ETA.ToString))))
                Else
                    sb.AppendFormat("<td class='bghighlight'>{0}</td>", Common.FormatDate(CDate(dr(EOSReportColumn.ETA.ToString))))
                End If


                If dr(EOSReportColumn.DocSent.ToString) Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", "No", bgcolor, IIf(Boolean.Equals("No", objLastStatus.DocSent), "", "class=boldText"))
                Else
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", IIf(CBool(dr(EOSReportColumn.DocSent.ToString)) = True, "Yes", "No"), bgcolor, IIf(Boolean.Equals(CBool(dr(EOSReportColumn.DocSent.ToString)), objLastStatus.DocSent), "", "class=boldText"))
                End If

                If dr(EOSReportColumn.PayReceived.ToString) Is System.DBNull.Value Then
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", "No", bgcolor, IIf(Boolean.Equals("No", objLastStatus.PayReceived), "", "class=boldText"))
                Else
                    sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", IIf(CBool(dr(EOSReportColumn.PayReceived.ToString)) = True, "Yes", "No"), bgcolor, IIf(Boolean.Equals(CBool(dr(EOSReportColumn.PayReceived.ToString)), objLastStatus.PayReceived), "", "class=boldText"))
                End If

                Dim strInvoiceNo As String = IIf(dr(EOSReportColumn.InvoiceNo.ToString) Is System.DBNull.Value, "", dr(EOSReportColumn.InvoiceNo.ToString))
                sb.AppendFormat("<td bgcolor={1} {2}>{0}</td>", strInvoiceNo, bgcolor, IIf(String.Equals(strInvoiceNo, objLastStatus.InvoiceNo), "", "class=boldText"))

                objLastStatus = Nothing
            End If

            sb.Append("</tr>")
        Next

        If Me.Output = Report.EReportOutputType.Screen Then
            sb.Insert(0, "<TBODY>")
            sb.Append("</TBODY>")
        End If

        Return sb.ToString
    End Function

    Private Function BuildDataTableOSReport(ByVal dt As DataTable) As DataTable

        Me._dataTableOSReport.Rows.Clear()
        Dim dr As DataRow

        For Each dr In dt.Rows
            SaveOrderStatusReportRow(Me._dataTableOSReport, dr)
        Next

        Return Me._dataTableOSReport
    End Function

    Protected Function GetDataView(ByVal dt As DataTable, ByVal sortExpression As String, ByVal sortDirection As String) As DataView
        Dim dv As DataView = New DataView(dt)
        dv.Sort = sortExpression & sortDirection

        Return dv
    End Function

    Private Function CreateDataTable(ByVal tableName As String) As DataTable

        ' Create a new DataTable.
        Dim myDataTable As DataTable = New DataTable(tableName)
        Dim myDataColumn As DataColumn

        ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
        'IsIndependentPO
        myDataColumn = New DataColumn(EOSReportColumn.IsIndependentPO.ToString(), System.Type.GetType("System.Boolean"))
        myDataTable.Columns.Add(myDataColumn)

        'SortReference
        myDataColumn = New DataColumn(EOSReportColumn.SortReference.ToString(), System.Type.GetType("System.String"))
        myDataTable.Columns.Add(myDataColumn)

        'SortReference2
        myDataColumn = New DataColumn(EOSReportColumn.SortReference2.ToString(), System.Type.GetType("System.String"))
        myDataTable.Columns.Add(myDataColumn)

        'OrderDate
        myDataColumn = New DataColumn(EOSReportColumn.OrderDate.ToString(), System.Type.GetType("System.DateTime"))
        myDataTable.Columns.Add(myDataColumn)

        'Qty
        myDataColumn = New DataColumn(EOSReportColumn.Qty.ToString(), System.Type.GetType("System.String"))
        myDataTable.Columns.Add(myDataColumn)

        'PercentCompleted
        myDataColumn = New DataColumn(EOSReportColumn.PercentCompleted.ToString(), System.Type.GetType("System.Decimal"))
        myDataTable.Columns.Add(myDataColumn)

        'OrderNumber
        myDataColumn = New DataColumn(EOSReportColumn.OrderNumber.ToString(), System.Type.GetType("System.String"))
        myDataTable.Columns.Add(myDataColumn)

        'ETD
        myDataColumn = New DataColumn(EOSReportColumn.ETD.ToString(), System.Type.GetType("System.DateTime"))
        myDataTable.Columns.Add(myDataColumn)

        'PREVETD
        myDataColumn = New DataColumn(EOSReportColumn.PREVETD.ToString(), System.Type.GetType("System.DateTime"))
        myDataTable.Columns.Add(myDataColumn)

        'Vessel
        myDataColumn = New DataColumn(EOSReportColumn.Vessel.ToString(), System.Type.GetType("System.String"))
        myDataTable.Columns.Add(myDataColumn)

        'DestinationPort
        myDataColumn = New DataColumn(EOSReportColumn.DestinationPort.ToString(), System.Type.GetType("System.String"))
        myDataTable.Columns.Add(myDataColumn)

        'ETA
        myDataColumn = New DataColumn(EOSReportColumn.ETA.ToString(), System.Type.GetType("System.DateTime"))
        myDataTable.Columns.Add(myDataColumn)

        'PREVETA
        myDataColumn = New DataColumn(EOSReportColumn.PREVETA.ToString(), System.Type.GetType("System.DateTime"))
        myDataTable.Columns.Add(myDataColumn)

        'DocSent
        myDataColumn = New DataColumn(EOSReportColumn.DocSent.ToString(), System.Type.GetType("System.Boolean"))
        myDataTable.Columns.Add(myDataColumn)

        'PayReceived
        myDataColumn = New DataColumn(EOSReportColumn.PayReceived.ToString(), System.Type.GetType("System.Boolean"))
        myDataTable.Columns.Add(myDataColumn)

        'InvoiceNo
        myDataColumn = New DataColumn(EOSReportColumn.InvoiceNo.ToString(), System.Type.GetType("System.String"))
        myDataTable.Columns.Add(myDataColumn)

        'OrderID
        myDataColumn = New DataColumn(EOSReportColumn.OrderID.ToString(), System.Type.GetType("System.Int32"))
        myDataTable.Columns.Add(myDataColumn)

        'Status
        myDataColumn = New DataColumn(EOSReportColumn.Status.ToString(), System.Type.GetType("System.Byte"))
        myDataTable.Columns.Add(myDataColumn)

        Return myDataTable
    End Function

    Private Sub SaveOrderStatusReportRow(ByRef table As DataTable, ByVal row As DataRow)

        Dim objO As New COrder
        objO.Load(row("ORDER_ID_FK"))

        Dim myDataRow As DataRow
        myDataRow = table.NewRow()
        myDataRow(EOSReportColumn.IsIndependentPO.ToString()) = objO.IsIndependentPO
        myDataRow(EOSReportColumn.SortReference.ToString()) = objO.SortReference
        myDataRow(EOSReportColumn.SortReference2.ToString()) = objO.SortReference2
        myDataRow(EOSReportColumn.OrderDate.ToString()) = objO.OrderDate
        myDataRow(EOSReportColumn.Qty.ToString()) = String.Format("{0} x{1}", objO.Containers, CType(objO.ContainerType, EContainerType).ToString.Replace("_", " "))
        myDataRow(EOSReportColumn.PercentCompleted.ToString()) = row("PERCENT_COMPLETED")
        myDataRow(EOSReportColumn.OrderNumber.ToString()) = objO.OrderNumber
        myDataRow(EOSReportColumn.ETD.ToString()) = row("EST_TIME_DELIVERY")
        myDataRow(EOSReportColumn.PREVETD.ToString()) = row("PREV_EST_TIME_DELIVERY")
        myDataRow(EOSReportColumn.Vessel.ToString()) = row("VESSEL")
        myDataRow(EOSReportColumn.DestinationPort.ToString()) = objO.DestinationPort
        myDataRow(EOSReportColumn.ETA.ToString()) = row("EST_TIME_ARRIVAL")
        myDataRow(EOSReportColumn.PREVETA.ToString()) = row("PREV_EST_TIME_ARRIVAL")
        myDataRow(EOSReportColumn.DocSent.ToString()) = row("DOC_SENT")
        myDataRow(EOSReportColumn.PayReceived.ToString()) = row("PAY_RECEIVED")
        myDataRow(EOSReportColumn.InvoiceNo.ToString()) = row("INVOICE_NO")
        myDataRow(EOSReportColumn.OrderID.ToString()) = row("ORDER_ID_FK")
        myDataRow(EOSReportColumn.Status.ToString()) = row("STATUS")

        table.Rows.Add(myDataRow)
    End Sub

    Public Sub New(ByVal supplierID As Integer, ByVal ETAportID As Integer, ByVal customerID As Integer, ByVal year As Integer, ByVal week As Integer, ByVal out As Report.EReportOutputType)
        Me._ETAportID = ETAportID
        Me._supplierID = supplierID
        Me._customerID = customerID
        Me._week = week
        Me._year = year
        Me._output = out
        Me._ETAPorts = New System.Web.UI.WebControls.ListItemCollection
        Me._Suppliers = New System.Web.UI.WebControls.ListItemCollection

        Me._dataTableOSReport = CreateDataTable("OrderStatusReport")
    End Sub

    Public Sub New()
        Me._ETAPorts = New System.Web.UI.WebControls.ListItemCollection
        Me._Suppliers = New System.Web.UI.WebControls.ListItemCollection

        Me._dataTableOSReport = CreateDataTable("OrderStatusReport")
    End Sub
End Class

Public Enum EOSReportColumn As Byte
    IsIndependentPO
    SortReference
    SortReference2
    OrderDate
    Qty
    PercentCompleted
    OrderNumber
    ETD
    PREVETD
    Vessel
    DestinationPort
    ETA
    PREVETA
    DocSent
    PayReceived
    InvoiceNo
    OrderID
    Status
End Enum

